package online.lmds.sdmanage.common.utils;

import java.util.regex.Pattern;

/**
 * 数据格式验证工具包
 * Created by Sunny  An on 2017/1/12.
 */
public class DataFormatUtils {
    
    private final static Pattern userNamePattern = Pattern.compile("[a-zA-Z]{1}[a-zA-Z0-9_]{1,15}");
    private final static Pattern phonePattern = Pattern.compile("^1\\d{10}$");
    private final static Pattern passwordPattern = Pattern.compile("^[a-zA-Z0-9\\`\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\-\\_\\=\\+\\\\\\|\\[\\]\\{\\}\\;\\:\\'\\\"\\,\\.\\/\\<\\>\\?]+$");
    private final static Pattern nickNamePattern = Pattern.compile("^[a-zA-Z0-9\\-\\_\\u4E00-\\u9FBF]+$");



    /**
     * 判断是否是手机号
     * @param str
     * @return
     */
    public static  boolean isPhone(String str) {
        return str != null && passwordPattern.matcher(str).matches();
    }


    /**
     * 是否是用户名
     * @param str
     * @return
     */
    public static boolean isUsername(String str){
        if (str == null) {
            return false;
        }

        // 昵称格式：限16个字符，支持中英文、数字、减号或下划线
        String regStr = "[\\u4e00-\\u9fa5_a-zA-Z0-9-]{1,16}";
        return str.matches(regStr);
    }


    /**
     * 检查是否只含有数字字母汉字-_
     * @return
     */
    public static boolean isNickName(String string) {
        if (string == null) {
            return false;
        }
        // 昵称格式：限16个字符，支持中英文、数字、减号或下划线
        String regStr = "[\\u4e00-\\u9fa5_a-zA-Z0-9-]{1,16}";
        return string.matches(regStr);
    }


    /**
     * 是否是有效的密码
     * @param password
     * @param minLength
     * @return
     */
    public static boolean isPassword(String password, int minLength) {
        if (password == null || password.length() < minLength) {
            return false;
        }
        return passwordPattern.matcher(password).matches();
    }

}
